
auto.dcov = function(A, B, lag = 0){
       A = as.matrix(A)
       B = as.matrix(B)
       N1 = dim(A)[1]
       N2 = dim(B)[1]
       if(N1 != N2) stop("Different Row Lengths")
       lag = floor(as.numeric(lag))
  if(lag < 0) stop("lag must be non-negative integer")
  M = seq(1,lag,1)
  adcov = numeric(length(M))
  S2 = numeric(length(M))
  ns = numeric(length(M))
  iter = 1
  for(i in M){
       if(i == 0){
               A.temp = A
               B.temp = B
               }
       if(i < 0){
               A.temp = A[-c(1:(-i)) , ]
               B.temp = B[-c((N1-(-i)+1):N1) , ]
               }
       if(i > 0){
               A.temp = A[-c((N1-i+1):N1) , ]
               B.temp = B[-c(1:i) , ]
               }
       adcov[iter] = dcov(A.temp, B.temp)
	 S2[iter] = get.S2(A.temp,B.temp)$S2;
	 ns[iter] = length(A.temp);
       iter = iter + 1
       }
  structure(list(lag = M, adcov = adcov, S2 = S2, ns = ns))
 # adcov
}